package com.dream.keigezhushou.Activity.acty.play.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dream.keigezhushou.Activity.Uiutils.JsonParse;
import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.acty.personal.fragment.MyGameAdapter;
import com.dream.keigezhushou.Activity.acty.play.GameInfoActivity;
import com.dream.keigezhushou.Activity.acty.play.HotGameAdapter;
import com.dream.keigezhushou.Activity.bean.GameListBean;
import com.dream.keigezhushou.Activity.bean.Image;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *   最热玩法
 */

public class HotGameFragment extends Fragment {

    private String[][] images = new String[][]{
            {"file:///android_asset/img2.jpg", "250", "250"}
            , {"http://img3.douban.com/view/photo/photo/public/p2249526036.jpg", "640", "960"}
            , {"file:///android_asset/img3.jpg", "250", "250"}
            , {"file:///android_asset/img4.jpg", "250", "250"}
            , {"file:///android_asset/img5.jpg", "250", "250"}
            , {"file:///android_asset/img6.jpg", "250", "250"}
            , {"file:///android_asset/img7.jpg", "250", "250"}
            , {"file:///android_asset/img8.jpg", "250", "250"}
            , {"http://img4.douban.com/view/photo/photo/public/p2252689992.jpg", "1280", "800"}
    };

    private PullToRefreshListView mRefreshListView;
    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mRefreshListView = (PullToRefreshListView) UiUtils.inflateView(R.layout.layout_hot_video);

        mListView = mRefreshListView.getRefreshableView();
        initView();

        return mRefreshListView;
    }

    //初始化界面
    protected void initView() {

        initData();
        //ListView创建适配器
//        mListView.setAdapter(new MyGameAdapter(getActivity(), imagesList));
        loadData();
    }


    //网络请求数据
    public void loadData(){

        OkHttpUtils.get()
                .url(NetURL.RecommendGame)
                .addParams("type","1")
                .addParams("page","1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        Log.i("22","请求成功");
                        ArrayList<GameListBean> arrayList = JsonParse.jsonToArrayList(response,GameListBean.class);

                        mListView.setAdapter(new HotGameAdapter(getActivity(),arrayList));
                    }
                });
    }

    //初始化数据
    protected void initData() {

//        arrayList = new ArrayList<GameListBean>();
////        ArrayList<Image> singleList=new ArrayList<>();
//        //从一到7生成7条朋友圈内容，分别是1~7张图片
//        for (int i = 0; i < 7; i++) {
//            ArrayList<GameListBean.CoverBean> itemList = new ArrayList<GameListBean.CoverBean>();
//            for (int j = 0; j <= i; j++) {
//                itemList.add(new GameListBean.CoverBean([j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));
//            }
//            arrayList.add(itemList);
//        }

        //ListView的item的点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(getActivity(), GameInfoActivity.class);
                startActivity(intent);
            }
        });
    }
}
